package org.example.stackandqueue;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * https://leetcode-cn.com/problems/valid-parentheses/
 */
public class IsValid {

    public static void main(String[] args) {

        String s="{[]}";
        boolean result = solution1(s);
        System.out.println(result);
    }

    public static boolean solution1(String s){

        if(s.length()%2==1){
            return false;
        }
       Map<Character,Character> map = new HashMap<Character,Character>(){{
           put(')','(');
           put('}','{');
           put(']','[');
       }};
        // {[]}
       Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(map.containsKey(ch)){
                if(stack!=null && !stack.peek().equals(map.get(ch))){
                    return false;
                }
                stack.pop();
            }else{
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}
